package com.java110.mall.user.cmd.store;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.intf.user.IStoreV1InnerServiceSMO;
import com.java110.po.store.StorePo;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

@Java110Cmd(serviceCode = "storeApi.updateStoreState")
public class UpdateStoreStateCmd extends Cmd {
    @Autowired
    private IStoreV1InnerServiceSMO storeV1InnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        String env = MappingCache.getValue(MappingConstant.ENV_DOMAIN, "HC_ENV");

        if (!"PROD".equals(env)) {
            throw new CmdException("演示环境，不允许操作");
        }
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {


        Assert.hasKeyAndValue(reqJson, "storeId", "未包含商户信息");
        Assert.hasKeyAndValue(reqJson, "state", "未包含商户状态");

        StorePo storePo = new StorePo();
        storePo.setStoreId(reqJson.getString("storeId"));
        storePo.setState(reqJson.getString("state"));
        int flag = storeV1InnerServiceSMOImpl.updateStore(storePo);
        if (flag < 1) {
            throw new CmdException("修改失败");
        }
    }
}
